/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;

import static Data.DataManager.FILEPATHDATA;
import static Data.DataManager.convertToSP;
import NhanVien.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sinz
 */
public class StaffManagement {
    public static final String FOLDER = "DuLieu";
    public static final String FILE_DSNV = "dsnv.txt";
    public static final String FILE_DSIF = "dsif.txt";
    public static final String FILE_PATH_DSNV = FOLDER + File.separator + FILE_DSNV;
    public static final String FILE_PATH_DSIF = FOLDER + File.separator + FILE_DSIF;
    
    // hàm đọc danh sách nhân viên
    public static ArrayList<NhanVien> getDSNV() throws IOException{
        ArrayList<NhanVien> ds = new ArrayList<NhanVien>();
        try {
            FileInputStream Data = new FileInputStream(FILE_PATH_DSNV);
            Scanner input = new Scanner(Data, "UTF-8");
            while (input.hasNextLine()) {
                String info[] = input.nextLine().replace("\ufeff", "").split(";");
                ds.add(convertToNV(info));
            }
            Data.close();
            input.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return ds;
    }

    private static NhanVien convertToNV(String[] info) {
        return new NhanVien(info[0], info[1],info[2], Integer.parseInt(info[3]), Double.parseDouble(info[4]));
    }
    
    //hàm đọc danh sách thông tin nhân viên
    public static ArrayList<Info> getDSIF() throws IOException{
        ArrayList<Info> ds = new ArrayList<Info>();
        try {
            FileInputStream Data = new FileInputStream(FILE_PATH_DSNV);
            Scanner input = new Scanner(Data, "UTF-8");
            while (input.hasNextLine()) {
                String info[] = input.nextLine().replace("\ufeff", "").split(";");
                ds.add(convertToIF(info));
            }
            Data.close();
            input.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return ds;
    }

    private static Info convertToIF(String[] info) {
        return new Info(info[0], info[1], info[2], Boolean.parseBoolean(info[3]), info[4], info[5], info[6]);
    }

    public static String getMSNV() throws IOException {
        String newMSNV = "NV";
        ArrayList<NhanVien> ds = StaffManagement.getDSNV();
        int MS = Integer.parseInt(ds.get(ds.size()-1).getMSNV().replace("NV",""))+1;
        String MSS=MS+"";
        if (MSS.length()<3)MSS="0"+MSS ;
        newMSNV = newMSNV+MSS;
        return newMSNV;
    }
}
